package com.itany.cms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itany.cms.constant.DictConstant;
import com.itany.cms.constant.StatusConstant;
import com.itany.cms.dao.UserMapper;
import com.itany.cms.entity.User;
import com.itany.cms.entity.UserParam;
import com.itany.cms.exception.CodeErrorException;
import com.itany.cms.exception.RequestParameterErrorException;
import com.itany.cms.exception.UserExistException;
import com.itany.cms.exception.UserNotExistException;
import com.itany.cms.service.UserService;
import com.itany.cms.util.MD5Util;
import com.itany.cms.util.ParameterUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Dictionary;
import java.util.List;

@Service
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    @Override
    public User login(String loginName, String password, String code, String image) throws RequestParameterErrorException, CodeErrorException, UserNotExistException {
        if (ParameterUtil.isNull(loginName) || ParameterUtil.isNull(password) || ParameterUtil.isNull(image)) {
            throw new RequestParameterErrorException("请求参数有误");
        }
        if (!code.equals(image)) {
            throw new CodeErrorException("验证码不正确");
        }
        QueryWrapper<User> wrapper = new QueryWrapper();
        wrapper.eq("login_name", loginName)
                .eq("password", MD5Util.md5(password))
                .eq("status", StatusConstant.USER_STATUS_ENABLED);
        List<User> users = userMapper.selectList(wrapper);
        if (users.isEmpty()) {
            throw new UserNotExistException("账号或密码错误");
        }
        return users.get(0);
    }

    @Override
    public PageInfo<User> findByParams(User userParam, String pageNo, String pageSize) {
        if (ParameterUtil.isNull(pageNo)) {
            pageNo = DictConstant.USER_DEFAULT_PAGE_NO;
        }
        if (ParameterUtil.isNull(pageSize)) {
            pageSize = DictConstant.USER_DEFAULT_PAGE_SIZE;
        }
        PageHelper.startPage(Integer.parseInt(pageNo),Integer.parseInt(pageSize));
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if (!ParameterUtil.isNull(userParam.getNickname())) {
            wrapper.like("name", userParam.getNickname());
        }
        if (!ParameterUtil.isNull(userParam.getLoginName())) {
            wrapper.like("login_name", userParam.getLoginName());
        }
        if (!ParameterUtil.isNull(userParam.getLoginName())) {
            wrapper.like("email", userParam.getEmail());
        }
        if (!ParameterUtil.isNull(userParam.getLoginName())) {
            wrapper.like("role", userParam.getRole());
        }
        if (!ParameterUtil.isNull(userParam.getStartCreateDate()) || !ParameterUtil.isNull(userParam.getEndCreateDate())) {
            wrapper.between("create_date", userParam.getStartCreateDate(), userParam.getEndCreateDate());
        }
        if (!ParameterUtil.isNull(userParam.getStartLoginDate()) || !ParameterUtil.isNull(userParam.getEndLoginDate())) {
            wrapper.between("login_date", userParam.getStartLoginDate(), userParam.getEndLoginDate());
        }
        List<User> users = userMapper.selectList(wrapper);
        PageInfo<User> userPage = new PageInfo<>(users);
        return userPage;
    }


    @Override
    public void modifyStatus(String id, String status) throws RequestParameterErrorException {
        if (ParameterUtil.isNull(id) || ParameterUtil.isNull(status)) {
            throw new RequestParameterErrorException("请求参数有误");
        }
        User user = new User();
        user.setId(Integer.parseInt(id));
        user.setStatus(Integer.parseInt(status));
        if (user.getStatus() == StatusConstant.USER_STATUS_ENABLED) {
            user.setStatus(StatusConstant.USER_STATUS_DISABLED);
        } else {
            user.setStatus(StatusConstant.USER_STATUS_ENABLED);
        }
        userMapper.updateById(user);
    }

    @Override
    public void modifyName(String id, String nickname, String password, String role, String email) throws UserExistException, RequestParameterErrorException {
        if (ParameterUtil.isNull(id) || ParameterUtil.isNull(nickname) || ParameterUtil.isNull(password) || ParameterUtil.isNull(role) || ParameterUtil.isNull(email)) {
            throw new RequestParameterErrorException("请求参数有误");
        }
        QueryWrapper<User> wrapper = new QueryWrapper();
        wrapper.eq("nickname", nickname)
                .ne("id", Integer.parseInt(id));
        List<User> users = userMapper.selectList(wrapper);
        if (!users.isEmpty()) {

            throw new UserExistException("该用户已经存在");
        }
        User user = new User();
        user.setId(Integer.parseInt(id));
        user.setNickname(nickname);
        user.setEmail(email);
        user.setRole(role);
        user.setPassword(password);
        userMapper.updateById(user);
    }

    @Override
    public List<User> findAll() {
        List<User> users = userMapper.selectList(null);
        return users;
    }
}
